/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui;

import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

/**
 * @author stl
 *
 * Not used at the moment
 */
public class JDialogPreferences extends JDialog {
  private boolean okPressed =false;
//  private static final String lfWindow ="com.sun.java.swing.plaf.windows.WindowsLookAndFeel" ;
//  private static final String lfMetal ="javax.swing.plaf.metal.MetalLookAndFeel" ;
//  private static final String lfMotif ="com.sun.java.swing.plaf.motif.MotifLookAndFeel" ;

  private JPanel panel1 = new JPanel();
  private JLabel jLabelLookAndFeel = new JLabel();
  private JComboBox jComboBoxLookAndFeel = new JComboBox();
  private JButton jButtonOk = new JButton();
  private JButton jButtonCancel = new JButton();
  private GridBagLayout gridBagLayout1 = new GridBagLayout();

  public JDialogPreferences(Frame frame, String title, boolean modal) {
	super(frame, title, modal);
	setLocationRelativeTo( frame );
	try {
	  initialize();
	  pack();
	}
	catch(Exception ex) {
	  ex.printStackTrace();
	}
  }


  void initialize() throws Exception {
	panel1.setLayout(gridBagLayout1);
	jLabelLookAndFeel.setText("Look and feel");
	jButtonOk.setText("Ok");
	jButtonOk.addActionListener(new java.awt.event.ActionListener() {
	  public void actionPerformed(ActionEvent e) {
		jButtonOk_actionPerformed(e);
	  }
	});
	jButtonCancel.setText("Cancel");
	jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
	  public void actionPerformed(ActionEvent e) {
		jButtonCancel_actionPerformed(e);
	  }
	});
	this.addWindowListener(new java.awt.event.WindowAdapter() {
	  public void windowActivated(WindowEvent e) {
		this_windowActivated(e);
	  }
	});
	getContentPane().add(panel1);
	panel1.add(jLabelLookAndFeel,   new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0
			,GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 0, 5), 0, 0));
	panel1.add(jComboBoxLookAndFeel,   new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0
			,GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
	panel1.add(jButtonOk,   new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0
			,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
	panel1.add(jButtonCancel,     new GridBagConstraints(1, 2, 1, 1, 0.0, 0.0
			,GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
 
	LookAndFeelComboInfo comboLaf, currentLaf =null;
	LookAndFeelInfo[] laf =UIManager.getInstalledLookAndFeels();

	for ( int i =0; i < laf.length; i++ ) {
	  comboLaf =new LookAndFeelComboInfo( laf[i] );
	  jComboBoxLookAndFeel.addItem( comboLaf );
	  if ( comboLaf.getName().equals( UIManager.getLookAndFeel().getName() ) ) {
		currentLaf =comboLaf;
	  }
	}

	// Select the current laf
	jComboBoxLookAndFeel.setSelectedItem( currentLaf );
  }

  /**Overridden so we can exit when window is closed*/
  protected void processWindowEvent(WindowEvent e) {
	if (e.getID() == WindowEvent.WINDOW_CLOSING) {
	  cancel();
	}
	super.processWindowEvent(e);
  }

  /**Close the dialog*/
  void cancel() {
	dispose();
  }

  void this_windowActivated(WindowEvent e) {
	okPressed =false;
  }

  void jButtonOk_actionPerformed(ActionEvent e) {
	okPressed =true;
    setVisible( false );

	// Change the look of the all application
	try {
	  String lafClass =((LookAndFeelComboInfo)jComboBoxLookAndFeel.getSelectedItem()).getClassName();
	  UIManager.setLookAndFeel( lafClass );
	  SwingUtilities.updateComponentTreeUI(this);

	} catch(Exception e2) {
	  e2.printStackTrace();
	}
  }

  void jButtonCancel_actionPerformed(ActionEvent e) {
	okPressed =false;
    setVisible( false );
  }

  public boolean okHasBeenPressed() {
	return okPressed;
  }

  /**
   * To facilitate combo box item representation of UI Look and feel
   */
  private class LookAndFeelComboInfo extends LookAndFeelInfo {
	public LookAndFeelComboInfo( String name, String className ) {
	  super( name, className );
	}

	public LookAndFeelComboInfo( LookAndFeelInfo lafi ) {
	  super( lafi.getName(), lafi.getClassName() );
	}

	public String toString() {
	  return getName();
	}
  }

}
